﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LiveResults.Model
{
    public class EventClass
    {
        public EventClass()
        {
            Runners = new Dictionary<int, Runner>();
            RadioControls = new List<RadioControl>();
        }

        public string ClassName { get; set; }
        public int Order { get; set; }
        public double CourseLength { get; set; }
        public bool Upload { get; set; }

        public Dictionary<int,Runner> Runners { get; set; }
        public IList<RadioControl> RadioControls { get; set; }

        public override int GetHashCode()
        {
            return ClassName.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (!(obj is EventClass))
                return false;

            EventClass other = obj as EventClass;
            return other.ClassName.Equals(ClassName);
        }

        public bool DefinitionEquals(EventClass other)
        {
            if (Order != other.Order || CourseLength != other.CourseLength || RadioControls.Count != other.RadioControls.Count)
                return false;

            for (int i = 0; i < other.RadioControls.Count; i++)
            {
                if (!RadioControls[i].Equals(other.RadioControls[i]))
                    return false;
            }

            return true;
        }

        public OnlineLib.OnlineClass OnlineClass
        {
            get
            {
                OnlineLib.OnlineClass cl = new OnlineLib.OnlineClass();
                cl.ClassName = this.ClassName;
                cl.Controls = new List<OnlineLib.RadioControl>();
                if (RadioControls != null)
                {
                    foreach (RadioControl rc in RadioControls)
                    {
                        if (rc.Upload)
                        {
                            OnlineLib.RadioControl rco = new OnlineLib.RadioControl();
                            rco.Code = rc.ControlCode;
                            rco.Name = rc.ControlName;
                            cl.Controls.Add(rco);
                        }
                    }
                }
                return cl;
            }
        }

        public List<Runner> UpdateRunnersInClassAndGetChanges(Dictionary<int,Runner> runners, out List<Runner> deletedRunners)
        {
            List<Runner> updates = new List<Runner>();
            deletedRunners = new List<Runner>();

            Dictionary<int, int> processedRunnerIds = new Dictionary<int, int>();
            foreach (KeyValuePair<int,Runner> kvp in runners)
            {
                processedRunnerIds.Add(kvp.Key,kvp.Key);
                if (Runners.ContainsKey(kvp.Key))
                {

                }
                else
                {
                    /*New Runner*/
                    Runners.Add(kvp.Key, kvp.Value);
                    updates.Add(kvp.Value);
                }
            }

            foreach (KeyValuePair<int, Runner> kvp in Runners)
            {
                if (!processedRunnerIds.ContainsKey(kvp.Key))
                    deletedRunners.Add(kvp.Value);
            }

            return updates;
        }
    }
}
